﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Messaging;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace UnionPay
{
    /// <summary>
    /// This Class Provides the TreeView with extended functionalities like,
    /// Adding the HeaderText feature to Node, Setting the icon for TreeViewNode.
    /// </summary>

    public class TreeViewItemWithIcons : TreeViewItem
    {
        #region Global variables
        ImageSource iconSourceBefore = null;
        ImageSource iconSourceAfter = null;
        TextBlock textBlock;
        Image iconBefore;
        Image iconAfter;
        #endregion Global variables

        #region Constructors and Destructors
        public TreeViewItemWithIcons()
        {
            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;
            Header = stack;

            // Add the Icon before adding the text
            iconBefore = new Image();
            iconBefore.VerticalAlignment = VerticalAlignment.Center;
            iconBefore.Margin = new Thickness(0, 0, 4, 0);
            iconBefore.Source = iconSourceBefore;
            stack.Children.Add(iconBefore);

            //Add the HeaderText After Adding the icon
            textBlock = new TextBlock();
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            stack.Children.Add(textBlock);


            // Add the Icon before adding the text
            iconAfter = new Image();
            iconAfter.VerticalAlignment = VerticalAlignment.Center;
            iconAfter.Margin = new Thickness(4, 0, 0, 0);
            iconAfter.Source = iconSourceAfter;
            stack.Children.Add(iconAfter);
        }
        #endregion Constructors and Destructors
        #region Properties
        /// <summary>
        /// Gets/Sets the Selected Image for a TreeViewNode
        /// </summary>
        public ImageSource IconBefore
        {
            set
            {
                iconSourceBefore = value;
                iconBefore.Source = iconSourceBefore;
            }
            get
            {
                return iconSourceBefore;
            }
        }
        public ImageSource IconAfter
        {
            set
            {
                iconSourceAfter = value;
            }
            get
            {
                return iconSourceAfter;
            }
        }

        /// <summary>
        /// Gets/Sets the index in a TreeViewNode
        /// </summary>
        public int IndexInTreeView { get; set; }
        #endregion Properties
        #region Event Handlers
        /// <summary>
        /// Event Handler on UnSelected Event
        /// </summary>
        /// <param name="args">Eventargs</param>
        protected override void OnUnselected(RoutedEventArgs args)
        {
            base.OnUnselected(args);
            iconAfter.Source = null;
        }
        /// <summary>
        /// Event Handler on Selected Event 
        /// </summary>
        /// <param name="args">Eventargs</param>
        protected override void OnSelected(RoutedEventArgs args)
        {
            base.OnSelected(args);
            iconAfter.Source = iconSourceAfter;
        }

        /// <summary>
        /// Gets/Sets the HeaderText of TreeViewWithIcons
        /// </summary>
        public string HeaderText
        {
            set
            {
                textBlock.Text = value;
            }
            get
            {
                return textBlock.Text;
            }
        }
        #endregion Event Handlers
    }
}
